﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarTools
{
    /// <summary>
    /// BrushPicker.xaml 的交互逻辑
    /// </summary>
    public partial class BrushPicker : UserControl
    {
        public BrushPicker()
        {
            InitializeComponent();
        }

        public event EventHandler<BrushPickedEventArgs> BrushPicked;

        protected void OnBrushPicked(object sender, BrushPickedEventArgs e)
        {
            if (BrushPicked != null)
            {
                BrushPicked(sender, e);
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Border border = sender as Border;
            if (border != null)
            {
                OnBrushPicked(this, new BrushPickedEventArgs(border.Background));
            }

            e.Handled = true;
        }

        private bool isMinizeButtonEnabled = true;


        public bool IsMinizeButtonEnabled
        {
            get { return isMinizeButtonEnabled; }
            set
            {
                isMinizeButtonEnabled = value;
                if (isMinizeButtonEnabled == false)
                {
                    btnMinimize.Visibility = Visibility.Hidden;
                }
                else
                {
                    btnMinimize.Visibility = Visibility.Visible;
                }
            }
        }

        public event EventHandler<MouseButtonEventArgs> MinizedButtonClicked;

        protected void OnMinizedButtonClicked(object sender, MouseButtonEventArgs e)
        {
            if (MinizedButtonClicked != null)
            {
                MinizedButtonClicked(sender, e);
            }
        }

        private void btnMinimize_MousePreviewLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isMinizeButtonEnabled)
            {
                OnMinizedButtonClicked(this, e);
            }

            e.Handled = true;
        }

        public event EventHandler<MouseButtonEventArgs> CloseButtonClicked;

        protected void OnCloseButtonClicked(object sender, MouseButtonEventArgs e)
        {
            if (CloseButtonClicked != null)
            {
                CloseButtonClicked(sender, e);
            }
        }

        private void btnClose_MousePreviewLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnCloseButtonClicked(this, e);
            e.Handled = true;
        }

        private void baseBorder_MouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
    }

    public class BrushPickedEventArgs : EventArgs
    {
        public BrushPickedEventArgs(Brush pickedBrush)
        {
            this.pickedBrush = pickedBrush;

            this.cnName = BrushManager.GetCnName(pickedBrush);
            this.enName = BrushManager.GetEnName(pickedBrush);
        }

        private string cnName;

        /// <summary>
        /// 色彩的中文名称。只支持Brushes类中定义的名称，否则会返回null。
        /// </summary>
        public string CnName
        {
            get { return cnName; }
        }

        private string enName;

        /// <summary>
        /// 色彩英文名称。只支持Brushes类中定义的名称，否则会返回null。
        /// </summary>
        public string EnName
        {
            get { return enName; }
        }

        private Brush pickedBrush;

        /// <summary>
        /// 拾取的色彩。只支持Brushes类中定义的色彩。
        /// </summary>
        public Brush PickedBrush
        {
            get { return pickedBrush; }
            set { pickedBrush = value; }
        }

        public override string ToString()
        {
            return "色彩信息：\r\n　　中文名称：" + this.cnName +
                "\r\n　　英文名称：" + enName + 
                "\r\n　　十六进制：" + this.pickedBrush.ToString();
        }
    }
}
